import { createSlice } from '@reduxjs/toolkit'

const tabSlice = createSlice({
  name: 'tab',
  initialState: {
    isCollapse: false,
    tabList: [
      {
        path: '/',
        name: 'home',
        label: '首页'
      }
    ]
  },

  reducers: {
    collapseMenu: (state) => {
      state.isCollapse = !state.isCollapse
    },
    selectMenuList: (state, { payload: val }) => {
      let resultIndex = state.tabList.findIndex((item) => item.name === val.name)
      if (resultIndex < 0) {
        state.tabList.push(val)
      }
    },
    removeMenuList: (state, { payload: val }) => {
      let resultIndex = state.tabList.findIndex((item) => item.name === val.name)
      if (resultIndex > -1) {
        state.tabList.splice(resultIndex, 1)
      }
    }
  }
})

export const { collapseMenu, selectMenuList, removeMenuList } = tabSlice.actions
export default tabSlice.reducer
